<template>
  <div :style="{ padding: '0 0 32px 32px' }">
    <h4 :style="{ marginBottom: '20px' }">{{ title }}</h4>
    <div id="main" style="height: 400px"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: "Bar",
  props: {
    title: {
      type: String,
      default: "",
    },
    data: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      option: "",
      xAxisData: [],
      seriesData1: [],
      seriesData2: [],
    };
  },
  watch: {
    data: {
      handler(val) {
        this.refresh();
      },
      deep: true,
    },
  },
  mounted() {
    this.refresh();
  },
  methods: {
    init(data) {
      this.seriesData1 = [];
      this.seriesData2 = [];
      this.xAxisData = this.data.map((item) => {
        this.seriesData1.push(item.visitNumber);
        this.seriesData2.push(item.cutSuccessNumber);
        return item.specificDate;
      });
    },
    refresh() {
      this.init();
      const myChart = echarts.init(document.getElementById("main"));
      this.option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        legend: {
          data: ["访问量", "砍价成功数"],
        },
        xAxis: {
          type: "category",
          data: this.xAxisData,
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            name: "访问量",
            data: this.seriesData1,
            type: "bar",
          },
          {
            name: "砍价成功数",
            data: this.seriesData2,
            type: "bar",
          },
        ],
      };

      myChart.setOption(this.option);
    },
  },
};
</script>
